﻿using System;
using DevExpress.XtraEditors;
using System.Linq;

namespace Management
{
    public partial class frmKullaniciSrm : XtraForm
    {
        public frmKullaniciSrm()
        {
            InitializeComponent();
        }

        private void frmKullaniciDepo_Load(object sender, EventArgs e)
        {
            using (MikroEntities ent = new MikroEntities())
            {
                foreach (var item in (from d in ent.Srms select new { d.som_kod, d.som_isim }).ToList())
                {
                    cmbDepo.Properties.Items.Add(new ComboItem() { DisplayMember = item.som_isim, ValueMember = item.som_kod });
                    lstDepolar.Items.Add(new ComboItem() { DisplayMember = item.som_isim, ValueMember = item.som_kod }, false);
                }
            }
            yeni();
        }

        private void yeni()
        {
            txtKodu.Text = "";
            txtKodu.Tag = "";
            txtAdi.Text = "";
            cmbDepo.SelectedIndex = 0;
            for (int i = 0; i < lstDepolar.Items.Count; i++)
            {
                lstDepolar.Items[i].CheckState = System.Windows.Forms.CheckState.Unchecked;
            }
            txtKodu.Focus();
        }

        private void ShowListe(string filtre)
        {
            frmListe liste = new frmListe();
            liste.ListeTipi = frmListe.ListeTipleri.KullaniciListesi;
            liste.SetValueControl = txtKodu;
            if (liste.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string kod = txtKodu.Tag.ToString();
                using (MikroEntities ent = new MikroEntities())
                {
                    Kullanici kul = ent.Kullanicilar.Where(x => x.Kodu == kod).FirstOrDefault();
                    if (kul != null)
                    {
                        txtKodu.Text = kul.Kodu;
                        txtAdi.Text = kul.Adi;

                        string yetki = ent.KullaniciHaklari.Where(x => x.Parametre == "Srm" && x.KullaniciId == kul.ID).Select(x => x.Deger).FirstOrDefault();
                        if (!string.IsNullOrEmpty(yetki))
                        {
                            string[] depolar = yetki.Split('-');
                            if (depolar.Length > 1)
                            {
                                for (int i = 0; i < cmbDepo.Properties.Items.Count; i++)
                                {
                                    if (((ComboItem)cmbDepo.Properties.Items[i]).ValueMember.ToString() == depolar[0])
                                        cmbDepo.SelectedIndex = i;
                                }

                                for (int i = 0; i < lstDepolar.Items.Count; i++)
                                {
                                    for (int x = 1; x < depolar.Length; x++)
                                    {
                                        if (((ComboItem)lstDepolar.Items[i].Value).ValueMember.ToString() == depolar[x])
                                            lstDepolar.Items[i].CheckState = System.Windows.Forms.CheckState.Checked;
                                    }

                                }

                            }
                            else
                            {
                                for (int i = 0; i < cmbDepo.Properties.Items.Count; i++)
                                {
                                    if (((ComboItem)cmbDepo.Properties.Items[i]).ValueMember.ToString() == depolar[0])
                                        cmbDepo.SelectedIndex = i;
                                }
                            }
                        }
                    }
                }
            }
        }

        private void txtKodu_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.F10)
                ShowListe(txtKodu.Text);
        }

        private void txtKodu_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            ShowListe(txtKodu.Text);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            using (MikroEntities ent = new MikroEntities())
            {
                Kullanici kul = ent.Kullanicilar.Where(x => x.Kodu == txtKodu.Text).FirstOrDefault();
                if (kul != null)
                {
                    string deger = ((ComboItem)cmbDepo.SelectedItem).ValueMember.ToString();
                    for (int i = 0; i < lstDepolar.Items.Count; i++)
                    {
                        if (((ComboItem)cmbDepo.SelectedItem).ValueMember.ToString() != ((ComboItem)lstDepolar.Items[i].Value).ValueMember.ToString() && lstDepolar.Items[i].CheckState == System.Windows.Forms.CheckState.Checked)
                            deger += "-" + ((ComboItem)lstDepolar.Items[i].Value).ValueMember.ToString();
                    }
                    KullaniciHakki hak = ent.KullaniciHaklari.Where(x => x.Parametre == "Srm" && x.KullaniciId == kul.ID).FirstOrDefault();
                    if (hak == null)
                    {
                        hak = new KullaniciHakki() { KullaniciId = kul.ID, Deger = deger, Parametre = "Srm" };
                        ent.AddToKullaniciHaklari(hak);
                    }
                    else
                    {
                        hak.Deger = deger;
                    }
                    ent.SaveChanges();
                }
            }
            yeni();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            using (MikroEntities ent = new MikroEntities())
            {
                Kullanici kul = ent.Kullanicilar.Where(x => x.Kodu == txtKodu.Text).FirstOrDefault();
                if (kul != null)
                {
                    KullaniciHakki hak = ent.KullaniciHaklari.Where(x => x.Parametre == "Srm" && x.KullaniciId == kul.ID).FirstOrDefault();
                    if (hak != null)
                    {
                        ent.DeleteObject(hak);
                        ent.SaveChanges();
                    }
                }
            }
            yeni();
        }
    }
}
